﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class OrbitalCommand : MonoBehaviour {

	//public GameObject baseObject;
	public GameObject[] baseObjects;
	public Transform rallyPoint;
	public bool spawnEnable = true;

	public Vector3 randomness;
	public float spawnTime = 2.5f;
	public float spawnCounter = 1.0f;
	public int spawnSize = 0;
	public int spawnSizeMax = 100; 

	public Text outputTextStatus;

	void FixedUpdate() {

		if (!spawnEnable) return;

		spawnCounter += Time.fixedDeltaTime / spawnTime;

		if (spawnCounter > 1) {
			spawnCounter --;
			if (spawnSize >= spawnSizeMax) return;
			if (baseObjects == null) return;
			if (baseObjects.Length < 1) return;

			GameObject baseObject = baseObjects[Random.Range (0, baseObjects.Length)];

			Vector3 start = transform.position;
			start.x += Random.Range (-0.5f, 0.5f) * randomness.x;
			start.y += Random.Range (-0.5f, 0.5f) * randomness.y;
			start.z += Random.Range (-0.5f, 0.5f) * randomness.z;
			GameObject n = Instantiate(baseObject, start, baseObject.transform.localRotation * Quaternion.Euler (0, Random.Range (0, 360), 0)) as GameObject;
			n.name = "clone";			
			n.transform.SetParent(rallyPoint);
			n.SetActive(true);

			spawnSize++;

			if (null != outputTextStatus) outputTextStatus.text =  "Supply: " + spawnSize + " / " + spawnSizeMax + " Vehicles";

		}

	}


	public void remove(GameObject g) {
		Destroy (g);
		spawnSize--;
	}


	public void increaseSupply() {
		spawnSizeMax = (int)((spawnSizeMax + 1) * 1.414f);
		if (null != outputTextStatus) 
			outputTextStatus.text =  "Supply: " + spawnSize + " / " + spawnSizeMax + " Vehicles";
	}

	public void setSpawn(Toggle t) {
		spawnEnable = t.isOn;
	}


}
